package org.zfes.snowier.common.monitor.router;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zfes.snowier.common.monitor.biz.service.ClassService;
import org.zfes.snowier.common.monitor.biz.service.GcService;
import org.zfes.snowier.common.monitor.biz.service.MysqlAcutorService;
import org.zfes.snowier.common.monitor.biz.service.MysqlMonitorDataService;
import org.zfes.snowier.common.monitor.biz.service.ThreadService;
import org.zfes.snowier.common.monitor.biz.service.ZActuatorService;

@Controller
@RequestMapping ( "/monitor/handler/handledata" )
public class MonitorDataHandleController {
    @Autowired
    private GcService gcService;
    @Autowired
    private ClassService classService;
    @Autowired
    private ThreadService threadService;

    @Autowired
    private ZActuatorService actuatorService;
    @Autowired
    private  MysqlMonitorDataService mysqlMonitorService;

    @Scheduled(cron="0 0 0 * * ?")
    public void clearAll(){
 	   gcService.clearAll();
 	   threadService.clearAll();
 	   classService.clearAll();
 	  mysqlMonitorService.clearAll();
    }
    
    @RequestMapping(value = "/allJvmInfo",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody Object heapgc(
    		@RequestParam String instanceKey,
    		@RequestParam String heapgc,
    		@RequestParam String clazz,
    		@RequestParam String thread,
    		@RequestParam String server){
    	System.out.println("-----allJvmInfo-instanceKey---- " +instanceKey);
    	System.out.println("-----allJvmInfo----heapgc------ "+heapgc);
    	System.out.println("-----allJvmInfo---clazz-------- "+clazz);
    	System.out.println("-----allJvmInfo---thread------- "+thread);
    	System.out.println("-----allJvmInfo---server- "+server);
    	
    	 gcService.save(instanceKey,heapgc);
    	 classService.save(instanceKey,clazz);
    	 threadService.save(instanceKey,thread);
    	 actuatorService.createInstance(instanceKey,server);
    	// System.out.println("-----heapgc-----"+heapgc);
        return "ok";
    }
    
    
    @RequestMapping("/mysqlMonitor")
    public  @ResponseBody Object mysqlMonitor(@RequestParam String instanceKey,@RequestParam String mysqlMonitorInfo){
    	System.out.println("-----mysqlMonitor-----"+mysqlMonitorInfo);
    	mysqlMonitorService.save(instanceKey, mysqlMonitorInfo);
    	return "ok";
    }
    
    @RequestMapping(value = "/heapgc",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody Object heapgc(@RequestParam String instanceKey,@RequestParam String heapgc){
    	 gcService.save(instanceKey,heapgc);
    	// System.out.println("-----heapgc-----"+heapgc);
        return "ok";
    }

    @RequestMapping(value = "/clazz",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody Object  clazz(@RequestParam String instanceKey,@RequestParam String clazz){
         classService.save(instanceKey,clazz);
         //System.out.println("-----clazz-----"+clazz);
         return "ok";
    }
    @RequestMapping(value = "/thread",method=RequestMethod.POST,produces=MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody Object socketThread(@RequestParam String instanceKey,@RequestParam String thread){
    	threadService.save(instanceKey,thread);
    	//System.out.println("-----thread-----"+thread);
    	 return "ok";
    }

    
    @RequestMapping("/instanceInfo")
    public  @ResponseBody Object instanceInfo(@RequestParam String instanceKey,@RequestParam String instanceInfo){
    	actuatorService.createInstance(instanceKey,instanceInfo);
    	//System.out.println("-----threinstanceInfoad-----"+instanceInfo);
    	return "ok";
    }
    
    

    
}
